<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix='sec' uri='http://www.springframework.org/security/tags'%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
	<style type="text/css">
	    html, body{
	        margin:0;padding:0;border:0;width:100%;height:100%;overflow:hidden;
	    }    
    </style>
</head>
<body>
	<div class="mini-toolbar" style="border-bottom:0;padding:0px;">
	    <table style="width:100%;">
	        <tr>
	        	<td style="width: 100%;">
	        			订单编号：
	                <input id="ser_no" class="mini-textbox" emptyText="请输入订单编号" style="width:150px;" onenter="onKeyEnter"/>  
	                	客户姓名： 
	                <input id="cus_name" class="mini-textbox" emptyText="请输入姓名" style="width:150px;" onenter="onKeyEnter"/>   
	        	</td>
	            <td style="white-space:nowrap;" >
	                <a class="mini-button" onclick="search()" >查询</a>
	            </td>
	        </tr>
	    </table>
	</div>
	<div id="toolbar1" class="mini-toolbar" style="padding:2px;">
	    <table style="width:100%;">
	        <tr>
		        <td style="width:100%;">
		        	<sec:authorize url='/allocation_init_export_current.do'>
		        	<a class="mini-button" iconCls="icon-download" onclick="exportCurrent()">导出当前</a>
		        	</sec:authorize>
		        	<sec:authorize url='/allocation_init_export_all.do'>
                    <a class="mini-button" iconCls="icon-download" onclick="exportAll()">导出全部</a>
                    </sec:authorize>
                    <sec:authorize url='/allocation_center_l1.do'>
		            <a class="mini-button" iconCls="icon-redo" onclick="onUser" plain="true">批量分配</a>
		            </sec:authorize>
		            <sec:authorize url='/allocation_center_l2.do'>
		            <a class="mini-button" iconCls="icon-redo" onclick="onCenter" plain="true">分配到中心</a>
		            </sec:authorize>
		            <sec:authorize url='/allocation_center_l3.do'>
		            <a class="mini-button" iconCls="icon-redo" onclick="onGroup" plain="true">分配到组</a>
		            </sec:authorize>
		        </td>
	        </tr>
	    </table>
	</div>
<!-- 	批量分配弹窗 -->
	<div id="win1" class="mini-window" allowDrag="false" allowResize="false" showHeader="true" title="订单分配--选择核单员" style="width: 650px;height: 360px;">
    	 <div style="text-align:center;line-height:23px;padding:5px;">
	         <span>选择中心：</span>    
	         <input emptyText="请选择中心" class="mini-combobox" style="width:180px;"
		 		id="orders_user_center" textField="name" valueField="id" url="" onvaluechanged="loadGroups();"/>
		 	<span style="margin-left: 10px;">选择核单组：</span>
	         <input emptyText="请选择核单组" class="mini-combobox" style="width:180px;"
		 		id="orders_user_group" textField="name" valueField="id" url="" onvaluechanged="loadUsers();"/>
    	 </div>
		<div style="padding-left:5px;padding-right:5px;">
	        <table cellpadding="0" cellspacing="0" >
	            <tr>
	                <td >
	                    <h4 style="margin:0;line-height:22px;font-size:13px;">核单员列表：</h4>
	                    <div id="orders_user" class="mini-datagrid" style="width:250px;height:200px;" showPageSize="false" showPageIndex="false"
	                        pagerStyle="padding:2px;" multiSelect="true" url="allocation_user_list.do" showReloadButton="false" showPager="false">
	                        <div property="columns">
	                            <div type="checkcolumn" ></div>
	                            <div field="name" width="100" headerAlign="center" >核单员</div>
	                            <div field="lineStatus" width="100" >在线状态</div>   
	                        </div>
	                    </div>
	                </td>
	                <td style="padding:5px;">
	           			<input type="button" value="选择" onclick="addSelectedUser()" style="width:50px;"/><br />
	                </td>
	                <td >
	                    <h4 style="margin:0;line-height:22px;font-size:13px;">已选核单员：</h4>
	                    <div id="selectedUser" class="mini-listbox" style="width:200px;height:200px;"              
	                            showCheckBox="true" multiSelect="true" >     
	                        <div property="columns">
	                            <div field="name" width="100" headerAlign="center" >核单员</div>    
	                        </div>
	                    </div>                       
	                </td>
	                <td style="padding:5px;">
			            <input type="button" value="删除" onclick="removeUsers()" style="width:55px;margin-bottom:2px;"/><br />                
			            <input type="button" value="清空" onclick="removeAllUsers()" style="width:55px;"/><br />                
	                </td>
	            </tr>
	        </table>
	    </div>
		<div style="text-align:center;padding: 20px;">
			<a class="mini-button" onclick="onOk1" style="">确定</a>
			<a class="mini-button" onclick="onClose1" style="margin-left: 50px;">关闭</a>
		</div>
	</div>
	<!-- 分配到中心弹窗 -->
	<div id="win2" class="mini-window" allowDrag="false" showHeader="true" title="分配到中心--选择中心" style="width: 310px;height: 150px;">
	    <div id="center" emptyText="请选择中心" labelField="true" label="中心：" labelStyle="width: 60px;" class="mini-combobox" style="width:250px; margin-left: 15px;margin-top: 10px;" popupWidth="170" textField="name" valueField="id" 
    		url="" multiSelect="true" showClose="true" oncloseclick="onCloseClick" >     
		    <div property="columns">
		    </div>
		</div>
		<div style="text-align:center;padding: 20px;">
			<a class="mini-button" onclick="onOk2" style="">确定</a>
			<a class="mini-button" onclick="onClose2" style="margin-left: 50px;">关闭</a>
		</div>
	</div>
<!-- 	分配到组弹窗 -->
	<div id="win3" class="mini-window" allowDrag="false" allowResize="false" showHeader="true" title="分配到组--选择核单组" style="width: 600px;height: 360px;">
    	 <div style="text-align:center;line-height:23px;padding:5px;">
	         <span>选择中心：</span>    
	         <input emptyText="请选择中心" class="mini-combobox" style="width:200px;"
		 		id="orders_center" textField="name" valueField="id" url="" onvaluechanged="loadGroup();"/>
    	 </div>
		<div style="padding-left:5px;padding-right:5px;">
	        <table cellpadding="0" cellspacing="0" >
	            <tr>
	                <td >
	                    <h4 style="margin:0;line-height:22px;font-size:13px;">核单组列表：</h4>
	                    <div id="orders_group" class="mini-datagrid" style="width:200px;height:200px;" showPageSize="false" showPageIndex="false"
	                        pagerStyle="padding:2px;" multiSelect="true" url="allocation_group_list.do" showReloadButton="false" showPager="false">
	                        <div property="columns">
	                            <div type="checkcolumn" ></div>
	                            <div field="name" width="100" headerAlign="center" >核单组</div>
	                        </div>
	                    </div>
	                </td>
	                <td style="padding:5px;">
	           			<input type="button" value="选择" onclick="addSelected()" style="width:50px;"/><br />
	                </td>
	                <td >
	                    <h4 style="margin:0;line-height:22px;font-size:13px;">已选核单组：</h4>
	                    <div id="selectedGroup" class="mini-listbox" style="width:200px;height:200px;"              
	                            showCheckBox="true" multiSelect="true" >     
	                        <div property="columns">
	                            <div field="name" width="100" headerAlign="center" >核单组</div>    
	                        </div>
	                    </div>                       
	                </td>
	                <td style="padding:5px;">
			            <input type="button" value="删除" onclick="removeSelecteds()" style="width:55px;margin-bottom:2px;"/><br />                
			            <input type="button" value="清空" onclick="removeAllSelecteds()" style="width:55px;"/><br />                
	                </td>
	            </tr>
	        </table>
	    </div>
		<div style="text-align:center;padding: 20px;">
			<a class="mini-button" onclick="onOk3" style="">确定</a>
			<a class="mini-button" onclick="onClose3" style="margin-left: 50px;">关闭</a>
		</div>
	</div>
	
	<div class="mini-fit" >
	<div id="datagrid1" class="mini-datagrid" onlyCheckSelection="true" idField="id" pageSize="20" url="allocation_view.do" style="width:100%;height:100%;" multiSelect="true">
		<div property="columns">
			<div type="indexcolumn"></div>
			<div type="checkcolumn"></div>
			<div name="action" width="70" headerAlign="center" align="center" renderer="onActionRenderer">相关操作</div>
			<div field="serialNum" width="100" headerAlign="center" align="center">订单编号</div>
			<div field="createTime" width="115" headerAlign="center" align="center" allowSort="true" dateFormat="yyyy-MM-dd HH:mm:ss">下单时间</div>
			<div field="name" width="55" headerAlign="center" align="center">收货人</div>
			<div field="address" width="160" headerAlign="center" align="center">收货地址</div>
			<div field="payment" width="60" headerAlign="center" align="center">货款金额</div>
			<div field="freight" width="60" headerAlign="center" align="center">运费</div>
			<div field="totalMoney" width="70" headerAlign="center" align="center">总金额</div>
		</div>
	</div>
	</div>
<script type="text/javascript">
	mini.parse();
	var grid = mini.get("datagrid1");
	grid.load({listType: 0});
	
	//导出当前
	function exportCurrent(){
	 	var data = grid.data;
        var ids = [];
        $.each(data , function(){
            ids.push(this.id);
        });
        var id = ids.join(',');
        location.href="allocation_init_export_current.do?ids=" +id;
        grid.reload();
	}
	
	//导出所有
	function exportAll(){
		var serNo = mini.get("ser_no").getValue();
		var cusName = mini.get("cus_name").getValue();
		location.href="allocation_init_export_all.do?serialNum="+serNo+"&name="+cusName+"&allocateGrade="+"0";
	}
	
	//相关操作
 	function onActionRenderer(e) {
 		var record = e.record;
    	return '<a style="width:150px;" href="javascript: ordersView('+record._uid+')">打开订单</a>';
    }
	
 	function ordersView(row_uid) {
    	var row = grid.getRowByUID(row_uid);
        mini.open({
            url: "orders_find_open_init.do?id=" + row.id,
            title: "打开订单", width: 850, height: 650,
            allowResize: false
        });
    }

	// 获取批量选择的数据
	function getSelectRows(){
		var rows = grid.getSelecteds();
		var id = [];
		if(rows.length > 0){
			var ids = [];
            for (var i = 0, l = rows.length; i < l; i++) {
                var r = rows[i];
                ids.push(r.id);
            }
            id = ids.join(',');
		}
		return id;
	}

	// 批量分配弹窗
	var win1 = mini.get("win1");
	var ordersUserCenter = mini.get("orders_user_center");
	var ordersUserGroup = mini.get("orders_user_group");
	function onUser(e){
		var ordersIds = getSelectRows();
		if(ordersIds.length > 0) {
			var el=e.sender.getEl();
			ordersUserCenter.load("allocation_center_list.do");
			win1.showAtEl(el, {
			   xAlign:"left",
			   yAlign:"below"
			});
		}else {
			mini.alert("未选择任何数据");
		}
	}
	
	function loadGroups() {
		ordersUser.clearRows();
		ordersUserGroup.load("allocation_group_list.do?centerId=" + ordersUserCenter.getValue());
	}
	
	var ordersUser = mini.get("orders_user");
	var selectedUser = mini.get("selectedUser");
	function loadUsers() {
		ordersUser.load({groupId: ordersUserGroup.getValue()});
	}
	
	function GetUserData() {
        var rows = selectedUser.getData();
        var ids = [], texts = [];
        for (var i = 0, l = rows.length; i < l; i++) {
            var row = rows[i];
            ids.push(row.id);
            texts.push(row.name);
        }

        var data = {};
        data.id = ids.join(",");
        data.text = texts.join(",");
        return data;
    }
	
	function addSelectedUser() {
        var items = ordersUser.getSelecteds();

        //根据id属性，来甄别要加入选中的记录
        var idField = ordersUser.getIdField();

        //把已选中的数据，用key-value缓存，以便进一步快速匹配
        var idMaps = {};
        var selecteds = selectedUser.getData();
        for (var i = 0, l = selecteds.length; i < l; i++) {
            var o = selecteds[i];
            var id = o[idField];
            idMaps[id] = o;
        }

        //遍历要加入的数组
        for (var i = items.length - 1; i >= 0; i--) {
            var o = items[i];
            var id = o[idField];
            if (idMaps[id] != null) items.removeAt(i);
        }
        ordersUser.removeRows(items);
        selectedUser.addItems(items);
    }
	
	function removeUsers() {
        var items = selectedUser.getSelecteds();
        selectedUser.removeItems(items);
        ordersUser.addRows(items);
    }
	
	function removeAllUsers() {
		var selecteds = selectedUser.getData();
		ordersUser.addRows(selecteds);
        selectedUser.removeAll();
    }
	
	function onOk1() {
		var ordersIds = getSelectRows();
		var selectedUsers = GetUserData();
		
		if(selectedUsers.id.length > 0) {
			$.post("allocation_allocate_user.do", {id: ordersIds, checkUserId: selectedUsers.id}, function(data){
				if(data.success) {
					mini.showMessageBox({
		                title: "提示",
		                showModal:false,
		                iconCls: "mini-messagebox-info",
		                message: "分配成功！",
		                timeout: 3000,
		                x: 'right',
		                y: 'bottom',
		            });
					grid.reload();
					ordersUser.clearRows();
					selectedUser.removeAll();
					ordersUserCenter.setValue("");
					ordersUserGroup.setValue("");
					win1.hide();
				}else {
					mini.alert("数据错误（待分配任务id或核单员id不能为空）");
				}
			}, "json");
		} else {
			mini.alert("请选择核单员！");
		}
	}
	
	function onClose1(e){
		ordersUser.clearRows();
		selectedUser.removeAll();
		ordersUserCenter.setValue("");
		ordersUserGroup.setValue("");
		win1.hide();
	}
	
	// 批量分配到中心弹窗
	var win2 = mini.get("win2");
	var centers = mini.get("center");
	function onCenter(e){
		var ordersIds = getSelectRows();
		if(ordersIds.length > 0){
			centers.load("allocation_center_list.do");
			var el=e.sender.getEl();
			win2.showAtEl(el, {
			   xAlign:"left",
			   yAlign:"below",
			});
		}else{
			mini.alert("未选择任何数据");
		}
	}

	function onCloseClick(e){
		var obj = e.sender;
        obj.setText("");
        obj.setValue("");
	}
	
	function onOk2(){
		var ordersIds = getSelectRows();
		var centersId = centers.getValue();
		if(centersId.length > 0){
	 		$.post("allocation_allocate_center.do", {id: ordersIds, centerId: centersId}, function(data){
				if(data.success){
					win2.hide();
					centers.setValue("");
					mini.showMessageBox({
		                title: "提示",
		                showModal:false,
		                iconCls: "mini-messagebox-info",
		                message: "分配成功！",
		                timeout: 3000,
		                x: 'right',
		                y: 'bottom'
		            });
					grid.reload();
				}else{
					mini.alert("分配失败（订单或中心id不能为空）");
				}
			}, "json");
		}else{
			mini.alert("请选择中心");
		}
	}
	
	function onClose2(e){
		centers.setValue("");
		win2.hide();
	}
	
	// 批量分配到组弹窗
	var win3 = mini.get("win3");
	var ordersCenters = mini.get("orders_center");
	function onGroup(e){
		var ordersIds = getSelectRows();
		if(ordersIds.length > 0) {
			ordersCenters.load("allocation_center_list.do");
			var el=e.sender.getEl();
			win3.showAtEl(el, {
			   xAlign:"left",
			   yAlign:"below"
			});
		}else {
			mini.alert("未选择任何数据");
		}
	}
	
	var ordersGroup = mini.get("orders_group");
	var selectedGroup = mini.get("selectedGroup");
	function loadGroup() {
		ordersGroup.load({centerId: ordersCenters.getValue()});
	}
	
	function GetData() {
        var rows = selectedGroup.getData();
        var ids = [], texts = [];
        for (var i = 0, l = rows.length; i < l; i++) {
            var row = rows[i];
            ids.push(row.id);
            texts.push(row.name);
        }

        var data = {};
        data.id = ids.join(",");
        data.text = texts.join(",");
        return data;
    }
	
	function addSelected() {
        var items = ordersGroup.getSelecteds();

        //根据id属性，来甄别要加入选中的记录
        var idField = ordersGroup.getIdField();

        //把已选中的数据，用key-value缓存，以便进一步快速匹配
        var idMaps = {};
        var selecteds = selectedGroup.getData();
        for (var i = 0, l = selecteds.length; i < l; i++) {
            var o = selecteds[i];
            var id = o[idField];
            idMaps[id] = o;
        }

        //遍历要加入的数组
        for (var i = items.length - 1; i >= 0; i--) {
            var o = items[i];
            var id = o[idField];
            if (idMaps[id] != null) items.removeAt(i);
        }
        ordersGroup.removeRows(items);
        selectedGroup.addItems(items);
    }
	
	function removeSelecteds() {
        var items = selectedGroup.getSelecteds();
        ordersGroup.addRows(items);
        selectedGroup.removeItems(items);
    }
	
	function removeAllSelecteds() {
	 	var items = selectedGroup.getData();
        ordersGroup.addRows(items);
        selectedGroup.removeAll();
    }
	
	function onOk3() {
		var ordersIds = getSelectRows();
		var selectedGroups = GetData();
		$.post("allocation_allocate_group.do", {id: ordersIds, groupId: selectedGroups.id}, function(data){
			if(data.success) {
				mini.showMessageBox({
	                title: "提示",
	                showModal:false,
	                iconCls: "mini-messagebox-info",
	                message: "分配成功！",
	                timeout: 3000,
	                x: 'right',
	                y: 'bottom',
	            });
				grid.reload();
				win3.hide();
				ordersGroup.clearRows();
				selectedGroup.removeAll();
				ordersCenters.setValue("");
			}else {
				mini.alert("数据错误（待分配订单或核单组id不能为空）");
			};
		}, "json");
	}
	
	function onClose3(e){
		win3.hide();
		ordersGroup.clearRows();
		selectedGroup.removeAll();
		ordersCenters.setValue("");
	}
	
	// 批量分配选择用户
	function allocateChoose(url, title) {
		var rows = grid.getSelecteds();
		if(rows.length > 0) {
			var ids = [];
            for (var i = 0, l = rows.length; i < l; i++) {
                var r = rows[i];
                ids.push(r.id);
            }
            var ordersId = ids.join(',');
			mini.open({
		        url: url,
		        showMaxButton: false,
		        title: title, width: 500,  height: 350,
		        onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = {ordersId: ordersId};
                    iframe.contentWindow.SetData(data);
                },
		        ondestroy: function (action) {
		        	if(action == 'succ'){
    	        		mini.showMessageBox({
    	                    title: "提示",
    	                    showModal:false,
    	                    iconCls: "mini-messagebox-info",
    	                    message: "分配成功！",
    	                    timeout: 3000,
    	                    x: 'right',
    	                    y: 'bottom'
    	                });
    		            grid.reload();
    	        	}
                    grid.reload();
		        }
		    });
		}else {
			mini.alert("未选择任何数据！");
		}
	}
    
    function search() {
    	var serNo = mini.get("ser_no").getValue();
		var cusName = mini.get("cus_name").getValue();
        grid.load({ listType: 0, serialNum: serNo, name: cusName });
    }
    
    function onKeyEnter(e) {
        search();
    }
</script>
</body>
</html>
